/**
 * 报表设计公司(2011)
 */
package com.reportgear.report.core.api.compare;

import java.awt.Image;
import java.text.Collator;
import java.util.Comparator;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 实用比较类
 * 
 * @version 1.0 2011-3-9
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 * 
 */
public class ComparatorUtils {
	private static final Logger logger = LoggerFactory.getLogger(ComparatorUtils.class.getName());

	public static int compare(Object obj1, Object obj2) {
		if ((obj1 == null) || (obj2 == null)) {
			return ((obj1 == null) ? -1 : ((obj1 == null) && (obj2 == null)) ? 0 : 1);
		}
		if ((obj1 instanceof Date) && (obj2 instanceof Date)) {
			long l1 = ((Date) obj1).getTime();
			long l2 = ((Date) obj2).getTime();
			return ((l1 > l2) ? 1 : (l1 == l2) ? 0 : -1);
		}
		if ((obj1 instanceof Number) && (!(obj2 instanceof Number))) {
			try {
				obj2 = Double.valueOf(obj2.toString());
			} catch (Throwable localThrowable1) {
				logger.warn("compare Error!");
			}
		}
		if ((obj2 instanceof Number) && (!(obj1 instanceof Number))) {
			try {
				obj1 = Double.valueOf(obj1.toString());
			} catch (Throwable localThrowable2) {
				logger.warn("compare Error!");
			}
		}
		if ((obj1 instanceof Number) && (obj2 instanceof Number)) {
			double d = ((Number) obj1).doubleValue() - ((Number) obj2).doubleValue();
			return ((d > 0.0D) ? 1 : (d == 0.0D) ? 0 : -1);
		}
		return obj1.toString().toLowerCase().compareTo(obj2.toString().toLowerCase());
	}

	public static int compare(Comparator<String> comparator, Object obj1, Object obj2) {
		if ((obj1 == null) || (obj2 == null)) {
			return ((obj1 == null) ? -1 : ((obj1 == null) && (obj2 == null)) ? 0 : 1);
		}
		if ((obj1 instanceof Date) && (obj2 instanceof Date)) {
			long l1 = ((Date) obj1).getTime();
			long l2 = ((Date) obj2).getTime();
			return ((l1 > l2) ? 1 : (l1 == l2) ? 0 : -1);
		}
		if ((obj1 instanceof Number) && (!(obj2 instanceof Number))) {
			try {
				obj2 = Double.valueOf(obj2.toString());
			} catch (Throwable localThrowable1) {
				logger.warn("compare Error!");
			}
		}
		if ((obj2 instanceof Number) && (!(obj1 instanceof Number))) {
			try {
				obj1 = Double.valueOf(obj1.toString());
			} catch (Throwable localThrowable2) {
				logger.warn("compare Error!");
			}
		}
		if ((obj1 instanceof Number) && (obj2 instanceof Number)) {
			double d = ((Number) obj1).doubleValue() - ((Number) obj2).doubleValue();
			return ((d > 0.0D) ? 1 : (d == 0.0D) ? 0 : -1);
		}
		if (comparator.getClass() == Collator.class) {
			return comparator.compare(obj1.toString().toLowerCase(), obj2.toString().toLowerCase());
		}
		return obj1.toString().toLowerCase().compareTo(obj2.toString().toLowerCase());
	}

	public static boolean equals(Object obj1, Object obj2) {
		if (obj1 == obj2) {
			return true;
		}
		if ((obj1 == null) || (obj2 == null)) {
			return ((obj1 == null) && (obj2 == null));
		}
		if ((obj1 instanceof Date) && (obj2 instanceof Date)) {
			return (((Date) obj1).getTime() == ((Date) obj2).getTime());
		}
		try {
			if ((obj1 instanceof Number) && (!(obj2 instanceof Number))) {
				obj2 = Double.valueOf(obj2.toString());
			}
			if ((obj2 instanceof Number) && (!(obj1 instanceof Number))) {
				obj1 = Double.valueOf(obj1.toString());
			}
		} catch (NumberFormatException localNumberFormatException) {
			return false;
		}
		if ((obj1 instanceof Number) && (obj2 instanceof Number)) {
			return (((Number) obj1).doubleValue() == ((Number) obj2).doubleValue());
		}
		return obj1.equals(obj2);
	}

	public static boolean equalsArray(Object[] arrObj1, Object[] arrObj2) {
		if ((arrObj1 == null) || (arrObj2 == null)) {
			return ((arrObj1 == null) && (arrObj2 == null));
		}
		if (arrObj1.length != arrObj2.length) {
			return false;
		}
		for (int i = 0; i < arrObj1.length; ++i) {
			if (!(equals(arrObj1[i], arrObj2[i]))) {
				return false;
			}
		}
		return true;
	}

	public static boolean equals_exactly(Object obj1, Object obj2) {
		if ((obj1 == null) || (obj2 == null)) {
			return ((obj1 == null) && (obj2 == null));
		}
		if ((obj1 instanceof Date) && (obj2 instanceof Date)) {
			return (((Date) obj1).getTime() == ((Date) obj2).getTime());
		}
		if ((obj1 instanceof Number) && (obj2 instanceof Number)) {
			return (((Number) obj1).doubleValue() == ((Number) obj2).doubleValue());
		}
		return obj1.equals(obj2);
	}

	public static boolean imageEquals(Image img1, Image img2) {
		throw new IllegalArgumentException("没有实现");
		// if ((img1 == null) || (img2 == null)) {
		// return ((img1 == null) && (img2 == null));
		// }
		// byte[] b1 = ObjectXML.encodeImage(img1);
		// byte[] b2 = ObjectXML.encodeImage(img2);
		// if (b1.length != b2.length) {
		// return false;
		// }
		// for (int i = 0; i < b1.length; ++i) {
		// if (b1[i] != b2[i]) {
		// return false;
		// }
		// }
		// return true;
	}
}
